package com.subsequent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsequent.dto.ScheduleBatchAddDTO;
import com.subsequent.pojo.ScheduleRules;
import com.subsequent.pojo.Schedules;
import com.subsequent.service.SchedulesService;
import com.subsequent.mapper.SchedulesMapper;
import com.subsequent.vo.ScheduleVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Time;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 19329
* @description 针对表【schedules(医生排班表)】的数据库操作Service实现
* @createDate 2025-05-07 09:48:49
*/
@Service
public class SchedulesServiceImpl extends ServiceImpl<SchedulesMapper, Schedules>
    implements SchedulesService{


    @Resource
    private SchedulesMapper scheduleMapper;

    @Override
    public List<ScheduleVO> querySchedules(String doctorName, Integer deptId, Integer status, Date startTime, Date endTime) {
        return scheduleMapper.querySchedules(doctorName, deptId, status, startTime, endTime);
    }

    @Override
    public List<ScheduleVO> querySchedules2(Integer doctorId, Integer deptId, String status, Integer serviceId, Date startTime, Date endTime) {
        return scheduleMapper.querySchedules2(doctorId, deptId, status, serviceId, startTime, endTime);
    }

    @Override
    public boolean batchAddSchedules(ScheduleBatchAddDTO dto) {
        List<Schedules> schedules = new ArrayList<>();
        LocalDate start = LocalDate.parse(dto.getStartDate());
        LocalDate end = LocalDate.parse(dto.getEndDate());
        for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
            for (ScheduleBatchAddDTO.ScheduleSetting setting : dto.getSettings()) {
                Schedules schedule = new Schedules();
                // schedule.setRuleId(...); // 如果你有规则ID生成逻辑
                schedule.setScheduleDate(Date.from(date.atStartOfDay()
                        .atZone(java.time.ZoneId.systemDefault())
                        .toInstant()));
                schedule.setStartTime(Time.valueOf(setting.getStartTime() + ":00")); // "08:00" -> "08:00:00"
                schedule.setEndTime(Time.valueOf(setting.getEndTime() + ":00"));     // "11:30" -> "11:30:00"
                schedule.setAvailableSlots(setting.getMaxAppointments()); // 用maxAppointments作为availableSlots
                schedule.setStatus(setting.getIsAvailable()); // 1-启用 0-停用
                schedule.setCreatedAt(new Date());
                schedule.setUpdatedAt(new Date());
                schedules.add(schedule);
            }
        }
        return scheduleMapper.insertBatchSomeColumn(schedules) > 0;
    }


}




